package io.renren.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.monitor.dao.TMonitorAlarmDao;
import io.renren.modules.monitor.entity.TMonitorAlarm;
import io.renren.modules.monitor.entity.vo.MonitorAlarmVO;
import io.renren.modules.monitor.service.MonitorAlarmService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * //Add the class description
 *
 * @author lhb
 * @date 2023/6/9
 */

@Service("monitorAlarmService")
public class MonitorAlarmServiceImpl extends ServiceImpl<TMonitorAlarmDao, TMonitorAlarm> implements MonitorAlarmService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        List<MonitorAlarmVO> alarmVOList = new ArrayList<>();
        String key = (String) params.get("key");
        if (StringUtils.isEmpty(key)) {
            alarmVOList = this.baseMapper.queryMonitorAlarms();
        } else {
            key = "%".concat(key).concat("%");
            alarmVOList = this.baseMapper.queryMonitorAlarmsByKeySearch(key);
        }
        return new PageUtils(alarmVOList, params);
    }

    @Override
    public boolean updateAlarm(String code, int eventId, int alarmStatus, Date alarmTime) {
        TMonitorAlarm monitorAlarm = this.baseMapper.getAlarmByCode(code, eventId);
        if (monitorAlarm == null) {
            monitorAlarm = new TMonitorAlarm();
            monitorAlarm.setCode(code);
            monitorAlarm.setEventId(eventId);
            monitorAlarm.setStatus(alarmStatus);
            monitorAlarm.setEventTime(alarmTime);
            return this.baseMapper.insert(monitorAlarm) > 0;
        } else {
            if (monitorAlarm.getStatus() != alarmStatus) {
                monitorAlarm.setStatus(alarmStatus);
                monitorAlarm.setDealTime(null);
            }
            monitorAlarm.setEventTime(alarmTime);
            return this.baseMapper.updateById(monitorAlarm) > 0;
        }
    }

	@Override
	public List<MonitorAlarmVO> queryMonitorAlarmsByEventTime(String startTime, String endTime) {
		return  this.baseMapper.queryMonitorAlarmsByEventTime(startTime, endTime);
	}
}
